﻿using AutoMapper.Internal.Mappers;
using Minio.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// OpenApi功能配置
    /// </summary>
    public class OpenApiFuncConfigService : CrudAppService<OpenApiFuncConfig, OpenApiFuncConfigDto, Guid, OpenApiFuncConfigInput, OpenApiFuncConfigCreateOrUpdateDto, OpenApiFuncConfigCreateOrUpdateDto>, IOpenApiFuncConfigService
    {
        private IRepository<OpenApiFuncConfig, Guid> _openApiFuncConfigRepository;

        public OpenApiFuncConfigService(IRepository<OpenApiFuncConfig, Guid> OpenApiFuncConfigRepository) : base(OpenApiFuncConfigRepository)
        {
            _openApiFuncConfigRepository = OpenApiFuncConfigRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<OpenApiFuncConfigDto>> GetListAsync(OpenApiFuncConfigInput input)
        {
            var iQuery = await _openApiFuncConfigRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.FuncCode), p => p.FuncCode == input.FuncCode);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OpenApiFuncConfig>, List<OpenApiFuncConfigDto>>(iQuery.ToList());

                return new PagedResultDto<OpenApiFuncConfigDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OpenApiFuncConfig>, List<OpenApiFuncConfigDto>>(items.ToList());

                return new PagedResultDto<OpenApiFuncConfigDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<OpenApiFuncConfigDto> CreateAsync(OpenApiFuncConfigCreateOrUpdateDto input)
        {
            OpenApiFuncConfig openApiFuncConfig = new OpenApiFuncConfig(input.ServiceName
                , input.MethodName, input.ParameterType, input.ReqType, input.FuncName
                , input.CatgeCodg, input.CatgeName, input.SrtNo, input.Descr);
            openApiFuncConfig = await _openApiFuncConfigRepository.InsertAsync(openApiFuncConfig);
            var itemDto = ObjectMapper.Map<OpenApiFuncConfig, OpenApiFuncConfigDto>(openApiFuncConfig);

            return itemDto;
        }
    }
}
